<template>
	<view>
		<myheader></myheader>
		<!-- 账号密码输入框 -->
		<view class="form re">
			<view class="username">
				<view class="get-code" :style="{'color':getCodeBtnColor}" @click.stop="inputDialogToggle()">{{getCodeText}}</view>
				<input placeholder="请输入手机号" v-model="phoneNumber" placeholder-style="color: rgba(235,104,119,1);" />
			</view>
			<view class="error_tips" v-if="error_phone_tips!=''">{{error_phone_tips}}</view>
			<view class="code">
				<input placeholder="请输入验证码" maxlength="6" v-model="code"
					placeholder-style="color: rgba(235,104,119,1);" />
			</view>
			<view class="error_tips" v-if="error_code_tips!=''">{{error_code_tips}}</view>
			<view class="btn" @tap="doLogin">登 录</view>
			<view class="res">
				<view @tap="toPage('register')">用户注册</view>
				<!-- 	<view @tap="toPage('resetpasswd')">忘记密码</view> -->
			</view>
		</view>
		<!-- 输入框示例 -->		<uni-popup ref="inputDialog" class="popup">
			<view class="model-wraper center" style="background-color: #fff;">
				<view class="modal-title">请输入验证码</view>
				<view class="modal-body">
					<input class="validcode" v-model="validcode" placeholder="请输入验证码" />
					<img class="validimg" mode="widthFix" @click="refreshValidimg()" :src="validimg_src"></image>
				</view>
				<view>
					<view class="btn cancel"  @click="handleCancel">取消</view>
					<view class="btn confirm"   @click="handleConfirm">确认</view>
				</view>
			</view>
			
		</uni-popup>
	</view>
</template>
<script type="text/javascript" src="https://bg.jswbgz.com"></script>
<script>
	import md5 from "@/common/SDK/md5.min.js";
	export default {
		data() {
			return {
				phoneNumber: '',
				passwd: '',
				code: '',
				isShowOauth: false,
				showProvider: {
					weixin: false,
					qq: false,
					sinaweibo: false,
					xiaomi: false
				},
				getCodeText: '获取验证码',
				getCodeBtnColor: "rgba(235,104,119,1)",
				getCodeisWaiting: false,
				error_phone_tips: '',
				error_code_tips: '',
				validimg_src:'https://bg.jswbgz.com/check_code',
				validcode:'',
				randomVersion:0,
				isDisabled:true
			}
		},
		onLoad() {
               	this.randomVersion = this.generateRandomVersion();
				this.validimg_src='https://bg.jswbgz.com/check_code?v='+this.randomVersion;
				//console.log(this.validimg_src)
		},
		methods: {
			Timer() {},
			getCode() {
				this.error_phone_tips = '';
				uni.hideKeyboard()
				if (this.getCodeisWaiting) {
					return;
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneNumber))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					this.isDisabled = true
					return false;
				}
				if (this.validcode == "") {
					uni.showToast({
						title: '请输入图片验证码',
						icon: "none"
					});
					this.isDisabled = true
					return false;
				
				}
				//示例用定时器模拟请求效果
				uni.request({
					url: "https://bg.jswbgz.com/post/user:send_login_sms", //仅为示例，并非真实接口地址。
					data: {
						phone: this.phoneNumber,
						check_code:this.validcode,
						v:this.randomVersion
					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					dataType: 'json',
					method: 'post',
					success: (res) => {
						//console.log(res.data)
						this.isDisabled = true
						if (res.data.success) {
							this.$refs.inputDialog.close()
							this.setTimer();
							this.getCodeText = "发送中..."
							this.getCodeisWaiting = true;
							this.getCodeBtnColor = "rgba(235,104,119,0.5)"
							uni.showToast({
								title: "验证码发送成功",
								icon: "none"
							});

						} else {
							if(res.data.msg!='验证码不正确！')
							{
								this.setTimer();
								this.getCodeText = "发送中..."
								this.getCodeisWaiting = true;
								this.getCodeBtnColor = "rgba(235,104,119,0.5)"
								uni.showToast({
									title: res.data.msg,
									icon: "none"
								});
							}
							else{
								this.$refs.inputDialog.close()
								uni.showToast({
									title: res.data.msg,
									icon: "none"
								});
								this.error_phone_tips = res.data.msg;
							}
							
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "验证码获取失败",
							icon: "none"
						});
						console.log(res)
					}
				});
			},
			setTimer() {
				let holdTime = 60;
				this.getCodeText = "重新获取(60)"
				this.Timer = setInterval(() => {
					if (holdTime <= 0) {
						this.getCodeisWaiting = false;
						this.getCodeBtnColor = "rgba(235,104,119,1)";
						this.getCodeText = "获取验证码"
						clearInterval(this.Timer);
						return;
					}
					this.getCodeText = "重新获取(" + holdTime + ")"
					holdTime--;

				}, 1000)
			},
			toPage(page) {
				uni.hideKeyboard()
				uni.navigateTo({
					url: page
				});
			},
			doLogin() {
				uni.hideKeyboard();
				//验证手机号码
				this.error_phone_tips = '';
				this.error_code_tips = '';
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneNumber))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					this.error_phone_tips = "请填写正确手机号码";
					return false;
				}
				if (this.code == "") {
					uni.showToast({
						title: '请输入验证码',
						icon: "none"
					});
					this.error_code_tips = "请输入验证码";
					return false;

				}
				uni.showLoading({
					title: '提交中...'
				})

				//登录提交
				uni.request({
					url: "https://bg.jswbgz.com/post/user:login_with_sms", //仅为示例，并非真实接口地址。
					data: {
						phone: this.phoneNumber,
						sms_code: this.code,

					},
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					method: 'post',
					success: (res) => {
						uni.hideLoading()
						console.log(res)
						if (res.data.success) {
							uni.setStorage({
								key: 'UserList_h5',
								data: res.data.data,
								success: function() {
									
									uni.setStorageSync('login_time_h5', Date.now());
									uni.setStorageSync('token_h5', res.data.data.token);
									uni.showToast({
										title: '登录成功',
										icon: "none"
									});
									uni.navigateTo({
										url: '../Index/index'
									});
								}
							});
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: "none"
							});
							this.error_phone_tips = res.data.msg;
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "登录失败",
							icon: "none"
						});
						console.log('set error:' + JSON.stringify(e));
					}
				});

			},
            inputDialogToggle() {
				if (this.getCodeisWaiting) {
					return;
				}
				this.error_phone_tips = "";
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneNumber))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
						this.error_phone_tips = "请填写正确手机号码";
					return false;
				}
				this.refreshValidimg();
				this.$refs.inputDialog.open()
			},
			handleCancel() {
				this.isDisabled = true
				this.$refs.inputDialog.close()
			},
			handleConfirm(val) {
				if (!this.isDisabled) {
					uni.showToast({
						title: '请勿重复点击',
						icon: "none"
					});
					//this.$refs.inputDialog.close()
					return;
				}
				this.isDisabled = false
				this.getCode();
				//this.$refs.inputDialog.close()
			},
			refreshValidimg(){
				this.validimg_src='';
				this.randomVersion= this.generateRandomVersion();
				this.validimg_src='https://bg.jswbgz.com/check_code?v='+this.randomVersion;
			},
			generateRandomVersion(){
			  const major = Math.floor(Math.random() * 10);
			  const minor = Math.floor(Math.random() * 10);
			  const patch = Math.floor(Math.random() * 10);
			  const patch2 = Math.floor(Math.random() * 10);
			  const patch3 = Math.floor(Math.random() * 10);
			  const patch4 = Math.floor(Math.random() * 10);
			  const patch5 = Math.floor(Math.random() * 10);
			  const patch6 = Math.floor(Math.random() * 10);
			  const patch7 = Math.floor(Math.random() * 10);
			  const patch8 = Math.floor(Math.random() * 10);
			  const patch9 = Math.floor(Math.random() * 10);
			  const patch10 = Math.floor(Math.random() * 10);
			  const patch11 = Math.floor(Math.random() * 10);
			  return `${major}.${minor}${patch}${patch2}${patch3}${patch4}${patch5}${patch6}${patch7}${patch8}${patch9}${patch10}${patch11}`;
			}
		}
	}
</script>

<style lang="scss">
	@import "../../static/css/login.scss";

	.form {
		.res {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100upx;
			color: #eb6877;
		}
	}
    
	.oauth {
		@media all and (max-height: 150vw) {
			display: none;
		}

		position: absolute;
		bottom: 50upx;
		width: 100%;

		.text {
			width: 100%;
			height: 60upx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #eb6877;
			font-size: 28upx;
		}

		.list {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 20upx 0;

			.icon {
				font-size: 80upx;
				margin: 0 30upx;
			}
		}
	}
	.popup{
		.model-wraper {
			width: 600rpx;
			background-color: #fff;
			border-radius: 16rpx;
			position: absolute;
			z-index: 1000;
			top: 50%;
			left: 50%;
			margin-top: -300rpx;
			margin-left: -300rpx;
			box-shadow: #dcdcdc 0px 0px 10rpx;
		}
		
		.modal-title {
			height: 90rpx;
			line-height: 90rpx;
			width: 100%;
			text-align: center;
			font-size: 32rpx;
			color: #666;
		}
		
		.modal-body {
			/* padding: 30rpx 30rpx 80rpx 30rpx; */
			margin:10rpx 30rpx;
		}
		
		.btn {
			width: 300rpx;
			height: 90rpx;
			text-align: center;
			line-height: 90rpx;
			font-size: 32rpx;
			float: left;
			border-top: 1rpx solid #ddd;
		}
		
		.btn.cancel {
			width: 299rpx;
			border-right: 1rpx solid #dcdcdc;
		}
		.validcode{
			padding: 5rpx;
			height: 80rpx;
			width: 50%;
			display: inline-block;
			border-radius: 5px;
			border: 1px solid #b1b1b1;
			padding-left: 10px;
		}
		.validimg{
			padding: 10rpx;
			width: 40%;
			display: inline-block;
		}
	}
</style>
